namespace SharpTalk.Core.Services.ServiceAgents
{
    using System;
    using Requests;
    using Responses;
    using System.ServiceModel;

    public sealed class UserServicesAgent
        : ServiceAgentBase<
        Contracts.Server.IUserServices,
        UserCreateRequest, UserCreateResponse,
        UserFindRequest, UserFindResponse,
        UserUpdateRequest, UserUpdateResponse,
        UserDeleteRequest,UserDeleteResponse>, 
        Contracts.Server.IUserServices
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="T:SharpTalk.Core.Services.ServiceAgents.UserServicesAgent"/> class using the configuration information specified in the application configuration file by <paramref name="endpointConfigurationName"/>. 
        /// </summary>
        /// <param name="endpointConfigurationName">The name of the endpoint in the application configuration file.</param><exception cref="T:System.ArgumentNullException">The specified endpoint information is null.</exception><exception cref="T:System.InvalidOperationException">The endpoint cannot be found or the endpoint contract is not valid.</exception>
        public UserServicesAgent(string endpointConfigurationName) 
            : base(endpointConfigurationName)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:SharpTalk.Core.Services.ServiceAgents.UserServicesAgent"/> class using the configuration information specified in the application configuration file by <paramref name="endpointConfigurationName"/>. 
        /// </summary>
        /// <param name="endpointConfigurationName">The name of the endpoint in the application configuration file.</param>
        /// <param name="remoteAddress"></param>
        /// <exception cref="T:System.ArgumentNullException">The specified endpoint information is null.</exception><exception cref="T:System.InvalidOperationException">The endpoint cannot be found or the endpoint contract is not valid.</exception>
        public UserServicesAgent(string endpointConfigurationName, EndpointAddress remoteAddress) 
            : base(endpointConfigurationName, remoteAddress)
        {
        }

        public IAsyncResult BeginNotifyUserChange(UserNotifyChangedRequest request, AsyncCallback callback, object state)
        {
            return base.Channel.BeginNotifyUserChange(request, callback, state);
        }

        public UserNotifyChangedResponse EndNotifyUserChange(IAsyncResult result)
        {
            try
            {
                return base.Channel.EndNotifyUserChange(result);
            }
            catch (Exception exp)
            {
                return new UserNotifyChangedResponse(){ Error = new ErrorDetails(exp)};
            }
        }
    }
}